package birdry.ui
{
	
	import birdry.manager.TweenManager;
	import birdry.utils.NetUtil;
	import com.greensock.TweenLite;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import birdry.utils.DisplayObjectUtil;
	
	/**
	* ...
	* @author 
	*/
	public class Menu extends Sprite	{
		
		public static const MENU_OVER:String = "menu_over";
		public static const MENU_OUT:String = "menu_out";
		
		var i:int;
		var total:int;
		var xmlData:XML;
		var selectNum:int;
		var arrowArr:Array = [37, 175, 342, 497, 613];
		
		public var overNum:int;
		
		public function Menu()	{
		}
		
		public function init(_xmlData:XML): void	{
			selectNum = Index.d1;
			xmlData = _xmlData;
			total = xmlData.menu.length();
			setLayout();
			addEvent();
			menuActive(selectNum-1);
		}
		
		private function setLayout(): void	{
			DisplayObjectUtil.allMouseEnabled(this);
			arrow.y = 39;
			for (i = 0; i < total; ++i)	{
				this["menu" + i].mc.txt.autoSize = "center";
				this["menu" + i].mc.txt.text = xmlData.menu[i].@name;
				this["menu" + i].num = i;
				this["menu" + i].hit.addEventListener(MouseEvent.MOUSE_OVER, function(e:MouseEvent): void	{
					var num:int = MovieClip(e.currentTarget.parent).num;
					menuOver(num);
				});
				this["menu" + i].hit.addEventListener(MouseEvent.MOUSE_OUT, function(e:MouseEvent): void	{
					menuOver(selectNum-1);
				});
				this["menu" + i].hit.addEventListener(MouseEvent.CLICK, function(e:MouseEvent): void	{
					var num:int = MovieClip(e.currentTarget.parent).num;
					NetUtil.navigateToURL(xmlData.menu[num].@link);
				});
			}
		}
		
		public function activeOpen(num:int): void	{
			menuActive(num);
		}
		
		public function activeClose(): void	{
			menuActive(selectNum-1);
		}
		
		private function menuActive(num:int): void	{
			var tScale:Number;
			var tY:Number;
			for (i = 0; i < total; ++i)	{
				if (i == num)	{
					tY = -4;
				}	else	{
					tY = 1;
				}
				TweenLite.to(this["menu" + i].mc, 0.4, { y:tY, ease:TweenManager.Expo_easeOut } );
			}
			if (num == -1)	{
				TweenLite.to(arrow, 0.4, { y:39, ease:TweenManager.Expo_easeOut } );
			}	else	{
				TweenLite.to(arrow, 0.4, { x:arrowArr[num], y:33, ease:TweenManager.Expo_easeOut } );
			}
		}
		
		private function menuOver(num:int = -1 ):void		{
			activeOpen(num);
			overNum = num;
			if (num != -1)	{
				dispatchEvent( new Event( Menu.MENU_OVER ));
			}	else	{
				dispatchEvent( new Event( Menu.MENU_OUT ));
			}
			
		}
		
		private function addEvent(): void	{
			
		}
		
	}	// class
	
}	// package